! 函数作为函数参数
module fdemo3

    implicit none

    abstract interface
        pure subroutine fcn(x, y, z)
            integer, intent(in) :: x, y
            integer, intent(out) :: z
        end subroutine fcn
    end interface

contains

    pure subroutine do_something(func, x, y, z)
        procedure(fcn) :: func
        integer, intent(in) :: x, y
        integer, intent(out) :: z

        call func(x, y, z)

    end subroutine do_something

end module fdemo3

program main

    use fdemo3
    implicit none

    integer :: x, y, z

    x = 1
    y = 2
    call do_something(add, x, y, z)
    print *, z

    x = 1
    y = 2
    call do_something(subtract, x, y, z)
    print *, z

contains

    pure subroutine add(x, y, z)
        integer, intent(in) :: x, y
        integer, intent(out) :: z
        z = x + y
    end subroutine add

    pure subroutine subtract(x, y, z)
        integer, intent(in) :: x, y
        integer, intent(out) :: z
        z = x - y
    end subroutine subtract

end program main
